<template>
  <div class="block-container container">
    <div class="title">考场管理</div>
    <div class="room-wrap" v-if="examRoomList.length">
      <div class="room-item" v-for="(item, idx) in examRoomList" :key="idx">
        <div class="room-title clearfix">
          <span>考点：{{ item.areaName }}</span>
          <span>考试日期：{{ `${item.beginDate} 至 ${item.endDate}` }}</span>
          <div class="opt pull-right">
            <span v-if="item.isExamRoom == '0'"  @click="showExpandTable(idx, item.areaName)">
              <a>{{ item.expand ? '收起' : '展开' }}</a>
            </span>
            <a href="javascript:;" @click="createExamRoom(item.areaName, null, item.pointId)">
              <i class="el-icon-circle-plus"></i> 增加考场
            </a>
          </div>
        </div>
        <div class="table-wrapper" v-show="item.expand">
          <el-table :data="item.tableData" border class="c-table" style="width: 100%" v-loading="item.loading">
            <el-table-column type="index" width="70"></el-table-column>
            <el-table-column prop="roomCode" label="考场编号"></el-table-column>
            <el-table-column prop="roomName" label="考场地址"></el-table-column>
            <el-table-column prop="waitName" label="候考地址"></el-table-column>
            <el-table-column prop="seatNum" label="座位数"></el-table-column>
            <el-table-column prop="roomPlace" label="座位分布">
              <template slot-scope="scope">
                {{ `${scope.row.rowNum}*${scope.row.colNum}` }}
              </template>
            </el-table-column>
            <el-table-column label="分布图"></el-table-column>
            <el-table-column label="操作" width="120">
              <template slot-scope="scope">
                <i class="el-icon-edit i-con" @click="createExamRoom(item.areaName, scope.row)"></i>
                <i class="el-icon-delete i-con" @click="handleDeleteExamRoom(scope.row, scope.$index, idx)"></i>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
    <div v-else>
      <p style="text-align:center;line-height: 60px;font-size: 16px;">暂无数据</p>
    </div>
     <!-- 删除确认 -->
    <kw-delconfim :delVisible.sync="delDialog" @handleDelete="handleDelete"></kw-delconfim>
  </div>
</template>

<script>
import kwDelconfim from '@/components/DelConfim.vue'
export default {
  name: 'examRoom',
  components: {
    kwDelconfim
  },
  data () {
    return {
      examRoomList: [],
      delDialog: false,
      rowIdx: null,
      examRoomId: null,
      pIdx: null
    }
  },
  created () {
    this.getExamRoomList()
  },
  methods: {
    // 展开&收起考场列表
    showExpandTable (idx, name) {
      this.examRoomList[idx].expand = !this.examRoomList[idx].expand
      if (this.examRoomList[idx].expand) {
        this.getExmaRoomByName(name, idx)
      }
    },
    // 获取考点列表
    getExamRoomList () {
      let that = this
      that.$http.getExamRoomList().then(res => {
        debugger
        if (res.code === 20000) {
          that.examRoomList = res.data.map((item, idx) => {
            let _item = item
            _item.expand = false
            _item.loading = false
            _item.tableData = []
            return _item
          })
          that.examRoomList.map((item, idx) => {
            if (item.isExamRoom === '0') {
              that.showExpandTable(idx, item.areaName)
            }
          })
          console.log('that.examRoomList', that.examRoomList)
        } else {
          that.$message.error(res.msg)
        }
      })
    },
    // 根据考点获取考场
    getExmaRoomByName (areaName, idx) {
      let that = this
      that.$http.getExamRoomByName({ areaName: areaName }).then(res => {
        if (res.code === 20000) {
          this.examRoomList[idx].tableData = res.data
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    createExamRoom (areaName, row = '', id="") {
      debugger
      if (row) {
        this.$router.push({
          name: 'setExaminationRoom',
          query: {
            area: areaName,
            id: row.id,
            code: row.roomCode,
            seat: row.seatNum,
            add: row.roomName,
            row: row.rowNum,
            col: row.colNum,
            wait: row.waitName
          }
        })
      } else {
        this.$router.push({ name: 'setExaminationRoom', query: { area: areaName, pointId: id } })
      }
    },
    // 删除指定考场
    handleDeleteExamRoom (row, ridx, pidx) {
      this.rowIdx = ridx
      this.pIdx = pidx
      this.examRoomId = row.id
      this.delDialog = true
    },
    // 删除回调 单个
    handleDelete () {
      let that = this
      that.$http.delExamRoom({ id: that.examRoomId }).then(res => {
        if (res.code === 20000) {
          this.examRoomList[this.pIdx].tableData.splice(that.rowIdx, 1)
          that.$message.success('删除考场成功')
        } else {
          that.$message.error(res.msg)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.table-wrapper{
  margin-top: 0;
}
.room-item {
  margin-bottom: 20px;
  .room-title{
    width: 100%;
    height: 50px;
    background-color: #3D68D5;
    color: #fff;
    box-sizing: border-box;
    padding: 0 30px;
    line-height: 50px;
    span{
      margin-right: 30px;
    }
    a{
      color: #fff;
      cursor: pointer;
      i{
        margin-right: 6px;
      }
    }
  }
}
.container {
  margin-left: 0;
  width: 100%;
}
</style>
